import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'

Vue.use(VueRouter)

const originPush = VueRouter.prototype.push
const originReplace = VueRouter.prototype.replace

VueRouter.prototype.push = function(loaction, onResolve, onReject) {
  if(onReject || onResolve) originPush.call(this, loaction,onResolve, onReject)
  return  originPush.call(this, loaction).catch(()=>{})
}
VueRouter.prototype.replace = function(loaction, onResolve, onReject) {
  if(onReject || onResolve) {
    originReplace.call(this, loaction,onResolve, onReject)
  } else {
    originReplace.call(this, loaction).catch(() => {})
  }
}

const router = new VueRouter({
    mode: 'history',
    routes,
    scrollBehavior(to, from, savedPosition) {
      if (savedPosition) {
        return savedPosition
      } else {
        return { x: 0, y: 0 }
      }
    }
})


export default router